package com.WISSC.serviceImpl;

import com.WISSC.bean.ExperienceUser;
import com.WISSC.bean.IndividualEvent;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.bean.layuiBean.VoPage;
import com.WISSC.mapper.IndividualEventMapper;
import com.WISSC.service.ExperienceUserService;
import com.WISSC.service.IndividualEventService;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 赛事个人信息表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Service
public class IndividualEventServiceImpl extends ServiceImpl<IndividualEventMapper, IndividualEvent> implements IndividualEventService {


    @Autowired
    ExperienceUserService experienceUserService;
    /**
     * @param current
     * @param size
     * @param name
     * @param idNumber
     * @param combat
     * @param raceAge
     * @param city
     * @return 查询 + 分页
     */
    @Override
    public ResultPage<IndividualEvent> selectMyPage(int current, int size,String name, Integer idNumber, Long birth, String combat, Integer raceAge, String city,HttpSession session,boolean admin) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (current - 1) * size);
        paramMap.put("limit", size);
        paramMap.put("name", name);
        paramMap.put("idNumber", idNumber);
        paramMap.put("birth", birth);
        paramMap.put("combat", combat);
        paramMap.put("raceAge", raceAge);
        paramMap.put("city", city);
        List<IndividualEvent> list = baseMapper.selectIndividualEvent(paramMap,admin);
        Integer count = baseMapper.countIndividualEvent(paramMap,admin);
        ResultPage<IndividualEvent> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }



    /**
     * @param individualEvent
     * @param session1
     * @return 个人信息登陆
     */
    @Override
    public boolean login(IndividualEvent individualEvent, HttpSession session1) {
        Map<String, Object> map = new HashMap<>();
        map.put("idNumber",individualEvent.getIdNumber());
        map.put("name",individualEvent.getName());
        List<IndividualEvent> users = baseMapper.login(map);
        //如果用户存在，放入session域
        if(users.size()>0) {
            session1.setAttribute("individualEvent", users.get(0));
            return  true;
        }else {
            return false;
        }
    }



    /**
     * @param
     * @return 个人赛事
     */
     /*@Override
     public List<IndividualEvent> selectByidNumber(String idNumber) {
         //Integer idNumber = ((IndividualEvent)session.getAttribute("individualEvent")).getIdNumber();

         List<IndividualEvent> list = baseMapper.selectByidNumber(idNumber);
         int integral=0;
         for ( int j = 0 ; j<list.size() ; j++){
             integral +=list.get(j).getIntegral();
         }
         list.get(0).setIntegral(integral);

         return list;
     }*/

    /**
     * @param individualEvent
     * @return 个人赛事添加
     */
    @Override
    public String individualEventInsert(String param2, IndividualEvent individualEvent) {
        Map map = new HashMap();
        //添加主表 + 次表
        if(param2 !="") {
            String[] str1 = param2.split("-");
            ArrayList list = new ArrayList<>();
            for (int i = 0; i < str1.length; i++) {
                list.add(str1[i]);
            }
            //个人信息主表 --创建日期
            individualEvent.setCreateTime(DateUtil.date());
            //个人信息 --添加
            if (this.save(individualEvent)) {
                for (int i = 0; i < list.size(); i++) {
                    String str = (String) list.get(i);
                    String[] arr = str.split(",");
                    if (arr != null) {
                        for (int j = 0; j < 1; j++) {
                            if (arr[0].equals("NaN")) {
                                break;
                            }
                            ExperienceUser experienceUser = new ExperienceUser();
                            experienceUser.setMatchTime(Long.parseLong(arr[0]));
                            experienceUser.setMatchName(arr[1]);
                            experienceUser.setRanking(Integer.parseInt(arr[2]));
                            experienceUser.setIntegral(Integer.parseInt(arr[3]));
                            experienceUser.setIndId(individualEvent.getId());
                            experienceUser.setCreateTime(DateUtil.date());
                            experienceUserService.save(experienceUser);
                        }
                    } else {
                        map.put("flag", true);
                        map.put("msg", "添加成功");
                        return JSON.toJSONString(map);
                    }
                }
            } else {
                map.put("flag", false);
                map.put("msg", "添加失败");
                return JSON.toJSONString(map);
            }

        }else{
           // 添加主表
            if (!this.save(individualEvent)) {
                map.put("flag", false);
                map.put("msg", "添加失败");
                return JSON.toJSONString(map);
            }
        }
        map.put("flag", true);
        map.put("msg", "添加成功");
        return JSON.toJSONString(map);
    }


    @Override
    public ResultPage<IndividualEvent> selectMyPage(HttpSession session, VoPage voPage) {
        String idNumber = ((IndividualEvent) session.getAttribute("individualEvent")).getIdNumber();
        System.out.println("idNumber:"+idNumber);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("idNumber",idNumber);
        List<IndividualEvent> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<IndividualEvent> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

//    @Override
//    public IndividualEvent select(HttpSession session, Integer idNumber) {
//        return null;
//    }

    @Override
    public IndividualEvent select( String idNumber) {

        return baseMapper.select(idNumber);
    }

    /**
     * @param
     * @return 获取图片
     */
    @Override
    public List<IndividualEvent> selectByIdNumber(String idNumber) {
        return baseMapper.selectByIdNumber(idNumber);
    }

    /**
     * @param city
     * @param idNumber
     * @return 排名
     */
    @Override
    public List<IndividualEvent> country(String city, String idNumber) {
        String[] str = city.split("/");
        List list = new ArrayList();
        for (int i=0;i<str.length;i++){
            if(str[0]!=null){
                list.add(baseMapper.country(str[0],idNumber));
            }
            if(str[1]!=null){
                list.add(baseMapper.country(str[1],idNumber));
            }
            if(str[2]!=null){
                list.add(baseMapper.country(str[2],idNumber));
            }
        }
        return list;
    }

    @Override
    public  Map getTeamCity(Long id) {
        String str = "";
        String sum = "";
        Map map = new HashMap();
        List<IndividualEvent> list = baseMapper.getTeamCity(id);
        for (int i = 0; i < list.size(); i++) {

            str = list.get(i).getCity();

        }
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getMatchName()!=null){
                String res= "";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                res = simpleDateFormat.format(list.get(i).getMatchTime());
                sum += list.get(i).getIndId() + "," + res + "," + list.get(i).getMatchName() + "," + list.get(i).getRanking() + "," + list.get(i).getIntegral()  + "=";
            }
        }
        map.put("str", str);
        map.put("sum", sum);
//        String sum = str +"="+arr;
//        System.out.println("sum"+sum);
        return map;
    }

    /**
     * @param voPage
     * @param name
     * @param idNumber
     * @return 新 个人赛是查询
     */
    @Override
    public ResultPage<IndividualEvent> listIndividualEvent(VoPage voPage, String name, String idNumber) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("name",name);
        paramMap.put("idNumber",idNumber);
        List<IndividualEvent> list = baseMapper.listndividualEvent(paramMap);
        Integer count = baseMapper.countListndividualEvent(paramMap);
        ResultPage<IndividualEvent> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    //个人信息修改
    @Override
    public Map updateindividualEvent(IndividualEvent individualEvent, String param2) {
        if (this.updateById(individualEvent)) {
            String[] split = param2.split("-");
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                for (int j = 0; j < 1; j++) {
                    if (split[0].equals("undefined") || split[0]=="") {
                        break;
                    }
                    ExperienceUser experienceUser = new ExperienceUser();
                    experienceUser.setId(Long.parseLong(split1[0]));
                    experienceUser.setMatchTime(Long.parseLong(split1[1]));
                    experienceUser.setMatchName(split1[2]);
                    experienceUser.setRanking(Integer.parseInt(split1[3]));
                    experienceUser.setIntegral(Integer.parseInt(split1[4]));
                    if (!experienceUserService.updateById(experienceUser)) {
                        return ResponseUtil.getExceptionMap();
                    }
                }
            }
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    @Override
    public List<IndividualEvent> getIndividualEvent(String idNumber) {
        return baseMapper.getIndividualEvent(idNumber);
    }

}
